<style type="text/css">
    table td.ayat-arabic {
        font-family: myFirstFont;
        text-align: center;
        font-size: 25px;
        letter-spacing: 1px;
        vertical-align: middle;
        weight: 10%;
        line-height:40px;
    }
    table td.ayat {
        text-align: center;
        vertical-align: middle;
        weight: 10%;
        line-height:40px;
    }
</style>
        
<div class="row">
    <div class="col-lg-12">
        <h1 class="page-header">Home</h1>
    </div>
</div>

<form class="form-horizontal" method="post" action="">
    <div class="control-group">
        <label class="control-label" for="pertanyaan">Pertanyaan</label>
        <div class="controls">
            <input type="text" id="pertanyaan" name="keyword" placeholder="malaikat" class="input-xxlarge search-query">
            <button type="submit" name="search" class="btn">Search</button>
        </div>
    </div>
</form>

<?php
if (!isset($_SESSION['keyword'])) {
    $_SESSION['keyword'] = '';
}
        
$_SESSION['keyword'] = isset($_POST['keyword']) ? str_replace("'", "''", $_POST['keyword']) : str_replace("'", "''", $_SESSION['keyword']);

if (!empty($_SESSION['keyword'])):
$dataPerHalaman = 10;
if (!empty($_GET['page'])) {
    $hal       = $_GET['page'] - 1;
    $mulaiAwal = $dataPerHalaman * $hal;
} elseif (!empty($_GET['page']) and $_GET['page'] == 1) {
    $mulaiAwal = 0;
} elseif (empty($_GET['page'])) {
    $mulaiAwal = 0;
}
?>
<table class="table table-striped">
    <thead>
        <tr>
            <th class="head-table">Ayat</th>
            <th class="head-table">Terjemah Indonesia</th>
            <th class="head-table">Qur'an</th>
            <th class="head-table">Ayat</th>
        </tr>
    </thead>
    <tbody>
        <?php
        //tampil data
        $sql = "SELECT qti.sura, qti.aya, qti.text,
                (SELECT text FROM quran_text_uthmani qtu 
                 WHERE qti.sura = qtu.sura AND qti.aya = qtu.aya) as text_arabic 
                FROM quran_text_indonesia qti WHERE LOWER(qti.text) LIKE '%".$_SESSION['keyword']."%' limit ".$mulaiAwal.", ".$dataPerHalaman;
        foreach ($pdo->query($sql) as $row) :
        ?>
            <tr>
                <td class="ayat"><?php echo $row['aya'] ?></td>
                <td class="indonesia"><?php echo $row['text'] ?></td>
                <td class="arabic"><?php echo $row['text_arabic'] ?></td>
                <td class="ayat-arabic"><?php echo angkaToArabic($row['aya']) ?></td>
            </tr>
        <?php
        endforeach;
        ?>
    </tbody>
</table>
<div class="pagination">
    <ul>
    <?php
    $jumlahData = 0;
    $sql = "SELECT COUNT(*) FROM quran_text_indonesia WHERE LOWER(text) LIKE '%".$_SESSION['keyword']."%'";
    $result = $pdo->prepare($sql); 
    $result->execute(); 
    $jumlahData = $result->fetchColumn();
    if ($jumlahData > $dataPerHalaman) {
        $banyakHalaman = ceil($jumlahData/$dataPerHalaman);
        $halaman = isset($_GET['page']) ? $_GET['page'] : 1;
        $halaman = (int)$halaman;
        if ($halaman > 1) {
            echo '<li><a href="?menu=home&page='.($halaman - 1).'">Prev</a></li>';
        } else {
            echo '<li class="disabled"><a href="#">Prev</a></li>';
        }
        
        for($page = 1; $page <= $banyakHalaman; $page++) {
            
            if ($page == 1) {
                if ($page == $halaman) {
                    echo '<li class="active"><a href="?menu=home&page='.$page.'">'.$page.'</a></li>';
                } else {
                    echo '<li><a href="?menu=home&page='.$page.'">First</a></li>';
                }
            } 
            
            if ($page == $banyakHalaman) {
                if ($page == $halaman) {
                    echo '<li class="active"><a href="?menu=home&page='.$page.'">'.$page.'</a></li>';
                } else {
                    echo '<li><a href="?menu=home&page='.$page.'">Last</a></li>';
                }
            }
            
            if (($page >= $halaman - 3) && ($page <= $halaman + 3)) {
                if ($page == $banyakHalaman) {
                    continue;
                }
                if ($page == 1) {
                    continue;
                }
                if ($page !== 1 || $page !== $banyakHalaman) {
                    if ($page == $halaman) {
                        echo '<li class="active"><a href="?menu=home&page='.$page.'">'.$page.'</a></li>';
                    } else {
                        echo '<li><a href="?menu=home&page='.$page.'">'.$page.'</a></li>';
                    }
                } 
                
            }
        }
        
        if ($halaman < $banyakHalaman) {
            echo '<li><a href="?menu=home&page='.($halaman + 1).'">Next</a></li>';
        } elseif ($halaman >= $banyakHalaman) {
            echo '<li class="disabled"><a href="#">Next</a></li>';
        }
    }
    ?>
    </ul>
</div>
<?php
endif;
?>
